package kaiin;

import java.beans.Beans;
import java.io.IOException;

import java.net.URI;
import java.net.URISyntaxException;

import DBs.Syori;
import DBs.DBExec;

import java.sql.SQLException;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Registration extends HttpServlet implements Syori{

	public void doGet(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
		this.doPost(req, res);
	}

	/**
	 * post用メソッド
	 */
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		// レスポンスのエンコードの指定
		res.setContentType("text/html; charset=utf-8");
		// リクエストパラメータのエンコード指定
		req.setCharacterEncoding("utf-8");

		String sql = "select user_id from kokyaku";

		DBExec dbe = new DBExec();
		dbe.selectTable(sql, this, req, res);


	}

	public void syori(ResultSet rs, HttpServletRequest req, HttpServletResponse res){

		HttpSession session = req.getSession(true);

		//確認ページから入力フォームへ戻る際、session内にbeansがあるかどうかを確認し、
		//beansありなら値を引き継ぎ、beansなしなら作成する。
		KaiinBeans bean = new KaiinBeans();
		bean = (KaiinBeans)session.getAttribute("kaiin");

		if(bean == null){
			bean = new KaiinBeans();
		}

		//ビーンズ作成
		bean.setUser_id(req.getParameter("id"));
		bean.setPassword(req.getParameter("password"));
		bean.setPassword2(req.getParameter("password2"));
		bean.setSimei(req.getParameter("name"));
		bean.setHurigana(req.getParameter("ruby"));
		bean.setYear(req.getParameter("year"));
		bean.setMonth(req.getParameter("month"));
		bean.setDay(req.getParameter("day"));
		bean.setSeibetu(req.getParameter("sex"));
		bean.setYuubin1(req.getParameter("postcode1"));
		bean.setYuubin2(req.getParameter("postcode2"));
		bean.setTodouhuken(req.getParameter("prefecture"));
		bean.setJuusyo1(req.getParameter("address1"));
		bean.setJuusyo2(req.getParameter("address2"));
		bean.setMail(req.getParameter("email"));
		bean.setTel(req.getParameter("tel"));
		bean.setKeitai(req.getParameter("cell"));

		String flag = "true";

		String id = bean.getUser_id();
		String password = bean.getPassword();
		String password2 = bean.getPassword2();
		String name = bean.getSimei();
		String ruby = bean.getHurigana();
		String year = bean.getYear();
		String month = bean.getMonth();
		String day = bean.getDay();
		String sex = bean.getSeibetu();
		String postcode1 = bean.getYuubin1();
		String postcode2 = bean.getYuubin2();
		String prefecture = bean.getTodouhuken();
		String address1 = bean.getJuusyo1();
		String address2 = bean.getJuusyo2();
		String email = bean.getMail();
		String tel = bean.getTel();
		String cell = bean.getKeitai();

		RequestDispatcher rd = req.getRequestDispatcher("../src/jsp/kaiin/registration.jsp");

		//空白チェック

		StringBuilder sb = new StringBuilder();
		//sbの初期値
		sb.append("<p>必須項目が入力されていません！</p>");

		if(id.equals("")){
			sb.append("<p>新規ID</p>");
		}
		if(password.equals("")){
			sb.append("<p>パスワード</p>");
		}
		if(password2.equals("")){
			sb.append("<p>パスワード再入力</p>");
		}
		if(name.equals("")){
			sb.append("<p>お名前</p>");
		}
		if(ruby.equals("")){
			sb.append("<p>お名前（フリガナ）</p>");
		}
		if(year.equals("0000") || month.equals("00") || day.equals("00")){
			sb.append("<p>生年月日</p>");
		}
		if(sex.equals("")){
			sb.append("<p>性別</p>");
		}
		if(postcode1.equals("") || postcode2.equals("")){
			sb.append("<p>郵便番号</p>");
		}
		if(prefecture.equals("") || address1.equals("")){
			sb.append("<p>住所</p>");
		}
		if(email.equals("")){
			sb.append("<p>E-Mailアドレス</p>");
		}
		if(tel.equals("")){
			sb.append("<p>電話番号</p>");
		}
		if(cell.equals("")){
			sb.append("<p>携帯番号</p>");
		}

		try{
		//必須項目が空の時、登録情報入力のページに戻る
			if(!sb.toString().equals("<p>必須項目が入力されていません！</p>")){
				req.setAttribute("alt", sb.toString());
				rd.forward(req, res);
				return;
			}

		//id重複チェック
			while(rs.next()){
				if(id.equals(rs.getString("user_id"))){
					flag = "false";
					req.setAttribute("flag", flag);
					rd.forward(req, res);
					return;
				}
			}

		//ここにパスワード再入力チェックを書く
			if(!password.equals(password2)){
				req.setAttribute("passwordCheck", "false");
				rd.forward(req, res);
			}

			rd = req.getRequestDispatcher("../src/jsp/kaiin/registration_conf.jsp");


			session.setAttribute("kaiin", bean);


			rd.forward(req, res);

		}catch(IOException e){
			e.printStackTrace();
		}catch(ServletException e){
			e.printStackTrace();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}